import Vue from 'vue'

// base library
import {
  ConfigProvider,
  Layout,
  Input,
  InputNumber,
  Button,
  Switch,
  Radio,
  Checkbox,
  Select,
  Card,
  Form,
  Row,
  Col,
  Modal,
  Table,
  Tabs,
  Icon,
  Badge,
  Popover,
  Dropdown,
  List,
  Empty,
  Avatar,
  Breadcrumb,
  Steps,
  Spin,
  Menu,
  Drawer,
  Tooltip,
  Alert,
  Tag,
  Divider,
  DatePicker,
  TimePicker,
  Upload,
  Progress,
  Skeleton,
  Popconfirm,
  PageHeader,
  Result,
  Statistic,
  Descriptions,
  Space,
  message,
  notification,
  Tree,
  Cascader,
  Timeline,
  Carousel,
  Calendar,
  Pagination,
  TreeSelect,
  FormModel,
  BackTop
} from 'ant-design-vue'
import Viser from 'viser-vue'

// ext library
import VueCropper from 'vue-cropper'
import Dialog from '@/components/Dialog'
import MultiTab from '@/components/MultiTab'
import PageLoading from '@/components/PageLoading'
import PermissionHelper from '@/core/permission/permission'
import './directives/action'
import yButton from '@/plugins/y-button'
import vViewer from '@/plugins/v-viewer'
import { isEmpty } from '@/utils/util'
import store from '../store'
import GlobalSearch from '@/components/GlobalSearch/index'
import lodash from 'lodash'
import EventBus from '@/utils/bus'
import Print from 'vue-print-nb'
import infiniteScroll from 'vue-infinite-scroll'
import vueSimpleUploader from '@/plugins/vue-simple-uploader'
import VScaleScreen from 'v-scale-screen'

Vue.use(vueSimpleUploader)
Vue.use(VScaleScreen)
Vue.use(infiniteScroll)
Vue.use(Print)
// 自定义按钮
Vue.use(yButton)
// 图片预览插件
Vue.use(vViewer)
Vue.use(GlobalSearch)
Vue.use(ConfigProvider)
Vue.use(Layout)
Vue.use(Input)
Vue.use(Empty)
Vue.use(InputNumber)
Vue.use(Button)
Vue.use(Switch)
Vue.use(Radio)
Vue.use(Checkbox)
Vue.use(Select)
Vue.use(Card)
Vue.use(Form)
Vue.use(Row)
Vue.use(Col)
Vue.use(Modal)
Vue.use(Table)
Vue.use(Tabs)
Vue.use(Icon)
Vue.use(Badge)
Vue.use(Popover)
Vue.use(Dropdown)
Vue.use(List)
Vue.use(Avatar)
Vue.use(Breadcrumb)
Vue.use(Steps)
Vue.use(Spin)
Vue.use(Menu)
Vue.use(Drawer)
Vue.use(Tooltip)
Vue.use(Alert)
Vue.use(Tag)
Vue.use(Divider)
Vue.use(DatePicker)
Vue.use(TimePicker)
Vue.use(Upload)
Vue.use(Progress)
Vue.use(Skeleton)
Vue.use(Popconfirm)
Vue.use(PageHeader)
Vue.use(Result)
Vue.use(Statistic)
Vue.use(Descriptions)
Vue.use(Space)
Vue.use(Tree)
Vue.use(Timeline)
Vue.use(Cascader)
Vue.use(Carousel)
Vue.use(Calendar)
Vue.use(Pagination)
Vue.use(TreeSelect)
Vue.use(FormModel)
Vue.use(BackTop)
if (store.getters.multiTab) {
  // 混入销毁事件 用于关闭tab标签是 取消缓存
  Vue.mixin({
    methods: {
      removeKeepAlive () {
        // 手动删除keepAlive的缓存
        if (this.$vnode && this.$vnode.data.keepAlive) {
          if (
            this.$vnode.parent &&
            this.$vnode.parent.componentInstance &&
            this.$vnode.parent.componentInstance.cache
          ) {
            if (this.$vnode.componentOptions) {
              var key =
                this.$vnode.key == null
                  ? this.$vnode.componentOptions.Ctor.cid +
                  (this.$vnode.componentOptions.tag ? `::${this.$vnode.componentOptions.tag}` : '')
                  : this.$vnode.key
              var cache = this.$vnode.parent.componentInstance.cache
              var keys = this.$vnode.parent.componentInstance.keys
              if (cache[key]) {
                if (keys.length) {
                  var index = keys.indexOf(key)
                  if (index > -1) {
                    keys.splice(index, 1)
                  }
                }
                delete cache[key]
              }
            }
          }
          // 调用销毁 否则会一直添加新缓存
          this.$destroy()
        }
      }
    }
  })
  if (process.env.NODE_ENV === 'development') {
    Vue.mixin({
      activated: lodash.debounce(function () {
        const content = document.querySelector('.ant-pro-grid-content')
        if (content.children.length === 1) {
          console.log('重新加载')
          location.reload()
        }
      })
    })
  }
}

Vue.prototype.$isEmpty = isEmpty
Vue.prototype.$confirm = Modal.confirm
Vue.prototype.$message = message
Vue.prototype.$notification = notification
Vue.prototype.$info = Modal.info
Vue.prototype.$success = Modal.success
Vue.prototype.$error = Modal.error
Vue.prototype.$warning = Modal.warning
Vue.prototype.$eventBus = EventBus

Vue.use(Viser)
Vue.use(Dialog) // this.$dialog func
Vue.use(MultiTab)
Vue.use(PageLoading)
Vue.use(PermissionHelper)
Vue.use(VueCropper)

process.env.NODE_ENV !== 'production' && console.warn('[antd-pro] NOTICE: Antd use lazy-load.')
